package com.gy.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gy.controller.common.UserHolder;
import com.gy.entity.User;
import com.gy.properties.JwtProperties;
import com.gy.result.Result;
import com.gy.service.UserService;
import com.gy.user.LogoutRequest;
import com.gy.user.UserLoginRequest;
import com.gy.user.UserRegisterRequest;
import com.gy.vo.UpdatePasswordRequest;
import com.gy.vo.UserLoginVO;
import com.gy.vo.UserPersonalVO;
import com.gy.vo.UserResultCountVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
@RequestMapping("/user")
@Api(tags = "用户接口")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;


    @PostMapping("/login")
    @ApiOperation("用户登录")
    // 删掉 HttpServletRequest request 参数
    public Result<UserLoginVO> userLogin(@RequestBody UserLoginRequest userLoginRequest) {
        // 调用 service 时，不再传 request（后续会改 service 去掉 request 依赖）
        Result<UserLoginVO> userVoResult = userService.userLogin(userLoginRequest);
        return userVoResult;
    }



    /**
     * 用户退出登录
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("用户退出登录")
    public Result logout(@RequestBody LogoutRequest logoutRequest) {
        return userService.logout(logoutRequest);
    }


    /**
     * 用户注册
     */
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public Result<UserPersonalVO> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        log.info("注册的用户账号:{}", userRegisterRequest.getUserAccount());
        Result<UserPersonalVO> userVoResult = userService.userRegister(userRegisterRequest);
        return userVoResult;
    }


    @GetMapping("/personal/count")
    @ApiOperation("根据当前登录用户查询个人成果数量")
    public Result<UserResultCountVO> getUserPersonalCount() {
        return userService.getUserPersonalCount();
    }

    /**
     * 根据用户的user_id查询用户个人信息
     */
    @GetMapping("/personal/info")
    @ApiOperation("根据当前登录用户查询个人基本信息")
    public Result<UserPersonalVO> getUserPersonalInfo() {
        Long userId = UserHolder.getUser().getUserId();
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(User::getUserId, userId)
                .select(User::getAvatar,
                        User::getNickName,
                        User::getPhoneNumber,
                        User::getEmail,
                        User::getTitle,
                        User::getHighestDegree,
                        User::getDepartment);

        User userInfo = userService.getOne(queryWrapper);

        if (userInfo == null) {
            return Result.error("用户信息不存在");
        }

        UserPersonalVO userPersonalVO = new UserPersonalVO();
        BeanUtils.copyProperties(userInfo, userPersonalVO);

        return Result.success(userPersonalVO);
    }


    /**
     * 更改用户个人信息
     */
    @PostMapping("/personal/update")
    @ApiOperation("更改用户个人信息")
    public Result<UserPersonalVO> updateUserPersonalInfo(@RequestBody UserPersonalVO userPersonalVO) {

       return userService.updateUserById(userPersonalVO);

    }

    /**
     * 修改密码
     */
    @PostMapping("/personal/change-password")
    @ApiOperation("修改密码")
    public Result<String> changePassword(@RequestBody UpdatePasswordRequest changePasswordRequest) {
        Long userId = UserHolder.getUser().getUserId();
        return userService.changePassword(userId, changePasswordRequest);
    }

}
